package nu.aing.P20140512;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;


/**
 *  Constraints 
 *  -   preference1 will contain N elements, where N is between 2 and 50, inclusive. 
 *  -   Elements of preference1 will contain each of the numbers from 1 to N exactly once. 
 *  -   preference2 will contain N elements. 
 *  -   Elements of preference2 will contain each of the numbers from 1 to N exactly once. 
 * @author memekmaut@gmail.com
 */
public class TeamsSelection {
    
    public static void main(String[] args) {
        System.out.println(
                simulate(
                        new int[] {1,2,3,4}, 
                        new int[] {1,2,3,4} ) );
        System.out.println(
                simulate(
                        new int[] {3,2,1}, 
                        new int[] {1,3,2} ) );
        System.out.println(
                simulate(
                        new int[] {8, 7, 1, 2, 4, 5, 6, 3, 9}, 
                        new int[] {7, 2, 4, 8, 1, 5, 9, 6, 3} ) );
        System.out.println(
                simulate(
                        new int[] {1,2}, 
                        new int[] {1,2} ) );
    }
    
    public static String simulate(int[] pref1, int[] pref2) {
        
        StringBuffer sb = new StringBuffer();
        List<Integer> prefs = new LinkedList<Integer>();
        for(int i = 0; i < pref1.length; i++) {
            prefs.add(pref1[i]);
        }
        Collections.sort(prefs);
        
        boolean isFillTeamOneFirst = true;
        for(int i = 0; i < prefs.size(); i++) {
            int personel = prefs.get(i);
            
            int pref1Index = -1, pref2Index = -1;
            for(int a = 0; a < pref1.length; a++) {
                if(personel == pref1[a]) {
                    pref1Index = a;
                    break;
                }
            }
            
            for(int b = 0; b < pref2.length; b++) {
                if(personel == pref2[b]) {
                    pref2Index = b;
                    break;
                }
            }
            
            if(pref1Index <= pref2Index) {
                if(isFillTeamOneFirst) {
                    sb.append("1");
                    
                    if(prefs.size() % 2 != 0) {
                        isFillTeamOneFirst = true;
                    }
                    else {
                        isFillTeamOneFirst = false;
                    }
                }
                else {
                    sb.append("2");
                    isFillTeamOneFirst = true;
                }
            }
            else {
                sb.append("2");
                isFillTeamOneFirst = true;
            }
        }
        
        return sb.toString();
    }
}
